//
//  ViewController.m
//  CBPickerTest
//
//  Created by shuai pan on 2017/11/21.
//  Copyright © 2017年 foreveross. All rights reserved.
//

#import "ViewController.h"
#import "CBPic2ker.h"
#import "UILabel+HTFloat.h"
@interface ViewController ()<CBPickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *userIcon;
@property (strong, nonatomic) IBOutlet UIButton *selectPicker;
@property (strong, nonatomic) UILabel *userTitle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.userTitle];
    [self.selectPicker setTitle:NSLocalizedString(@"select picker", nil) forState:UIControlStateNormal];

    // Do any additional setup after loading the view, typically from a nib
}
- (IBAction)selectPicker:(UIButton *)sender {
    CBPhotoSelecterController *controller = [[CBPhotoSelecterController alloc] initWithDelegate:self];
    controller.columnNumber = 4;
    controller.maxSlectedImagesCount = 5;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)photoSelecterController:(CBPhotoSelecterController *)pickerController sourceAsset:(NSArray *)sourceAsset {
    __weak typeof(self) weakSelf = self;
    
    if (sourceAsset.count>0 && [sourceAsset[0] isKindOfClass:[PHAsset class]]) {
        PHAsset *asset = (PHAsset*)sourceAsset[0];
        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
            [weakSelf.userIcon setImage:[UIImage imageWithData:imageData]];
            id url = [info objectForKey:@"PHImageFileURLKey"];
            if ([url isKindOfClass:[NSURL class]]) {
                [self.userTitle heightToFit:((NSURL *)url).relativePath fontSize:14.f];
                [self.userTitle setText:((NSURL *)url).relativePath];
            }
            
        }];
//        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//            [weakSelf.userIcon setImage:result];
//                        NSLog(@"====== %@",info);
//
//        }];
    }
    
    
    
}
- (void)photoSelecterDidCancelWithController:(CBPhotoSelecterController *)pickerController {
    
}
- (UILabel *)userTitle {
    if (!_userTitle) {
        _userTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, CGRectGetWidth(self.view.bounds), 0.f)];
        _userTitle.font = [UIFont systemFontOfSize:14.f];
        _userTitle.numberOfLines = 0;
        _userTitle.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _userTitle;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
